{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7.1 Description of quicksort"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7.1-1\n",
    "\n",
    "> Using Figure 7.1 as a model, illustrate the operation of PARTITION on the array $A = \\left \\langle 13, 19, 9, 5, 12, 8, 7, 4, 21, 2, 6, 11 \\right \\rangle$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](img/7.1-1_1.png)\n",
    "\n",
    "![](img/7.1-1_2.png)\n",
    "\n",
    "![](img/7.1-1_3.png)\n",
    "\n",
    "![](img/7.1-1_4.png)\n",
    "\n",
    "![](img/7.1-1_5.png)\n",
    "\n",
    "![](img/7.1-1_6.png)\n",
    "\n",
    "![](img/7.1-1_7.png)\n",
    "\n",
    "![](img/7.1-1_8.png)\n",
    "\n",
    "![](img/7.1-1_9.png)\n",
    "\n",
    "![](img/7.1-1_10.png)\n",
    "\n",
    "![](img/7.1-1_11.png)\n",
    "\n",
    "![](img/7.1-1_12.png)\n",
    "\n",
    "![](img/7.1-1_13.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7.1-2\n",
    "\n",
    "> What value of $q$ does PARTITION return when all elements in the array $A[p \\dots r]$ have the same value? Modify PARTITION so that $q = \\left \\lfloor(p + r)/2 \\right \\rfloor$ when all elements in the array $A[p \\dots r]$ have the same value."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "PARTITION returns $r$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def partition(a, p, r):\n",
    "    x = a[r - 1]\n",
    "    i = p - 1\n",
    "    for k in range(p, r - 1):\n",
    "        if a[k] < x:\n",
    "            i += 1\n",
    "            a[i], a[k] = a[k], a[i]\n",
    "    i += 1\n",
    "    a[i], a[r - 1] = a[r - 1], a[i]\n",
    "    j = i\n",
    "    for k in range(i + 1, r):\n",
    "        if a[k] == x:\n",
    "            j += 1\n",
    "            a[j], a[k] = a[k], a[j]\n",
    "        k -= 1\n",
    "    return (i + j) // 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7.1-3\n",
    "\n",
    "> Give a brief argument that the running time of PARTITION on a subarray of size $n$ is $\\Theta(n)$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Only one loop."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7.1-4\n",
    "\n",
    "> How would you modify QUICKSORT to sort into nonincreasing order?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def partition(a, p, r):\n",
    "    x = a[r - 1]\n",
    "    i = p - 1\n",
    "    for j in range(p, r - 1):\n",
    "        if a[j] >= x:\n",
    "            i += 1\n",
    "            a[i], a[j] = a[j], a[i]\n",
    "    i += 1\n",
    "    a[i], a[r - 1] = a[r - 1], a[i]\n",
    "    return i\n",
    "\n",
    "\n",
    "def quicksort(a, p, r):\n",
    "    if p < r - 1:\n",
    "        q = partition(a, p, r)\n",
    "        quicksort(a, p, q)\n",
    "        quicksort(a, q + 1, r)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
